package com.steak.system.mq;
import com.steak.system.mapper.ApplyDOMapper;
import com.steak.system.mapper.UserDOMapper;
import com.steak.system.pojo.dataobject.ApplyDO;
import com.steak.system.pojo.dataobject.UserDO;
import com.steak.system.pojo.dto.ApplyDTO;
import com.steak.system.pojo.dto.CheckDTO;
import com.steak.system.pojo.dto.EmailDTO;
import com.steak.system.pojo.dto.InterviewResultDTO;
import com.steak.system.util.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Component
public class MQ {
    @Autowired
    private UserDOMapper userDOMapper;
    @Autowired
    private ApplyDOMapper applyDOMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 插入数据库（注册）
     * @param objectMessage
     * @throws JMSException
     */
    @JmsListener(destination = "${MyTopic}")
    public void noticeMQ(ObjectMessage objectMessage) throws JMSException {
        UserDO userDO = (UserDO) objectMessage.getObject();
        objectMessage.acknowledge();
        userDOMapper.insertSelective(userDO);
    }

    /**
     * 发送邮件
     * @param objectMessage
     * @throws JMSException
     */
    @JmsListener(destination = "${MyTopic}")
    public void noticeStu(ObjectMessage objectMessage) throws JMSException {

        UserDO userDO = (UserDO) objectMessage.getObject();
        System.out.println("注册邮件"+userDO.getEmail());
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(userDO.getEmail());
            simpleMailMessage.setSubject("实验室管理系统");
            simpleMailMessage.setText("欢迎你注册本系统，你的密码为"+userDO.getPassword()+",请牢记");
            javaMailSender.send(simpleMailMessage);
            System.out.println("消息发送成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 审核消息队列，去该数据库审核状态
     * @param objectMessage
     * @throws JMSException
     */
    @JmsListener(destination = "${MyTopic2}")
    public void changeStatus(ObjectMessage objectMessage) throws JMSException {
        CheckDTO checkDTO = (CheckDTO) objectMessage.getObject();
        ApplyDO applyDO = new ApplyDO();
        applyDO.setApplyId(checkDTO.getApplyId());
        if (checkDTO.getStatus().equals("ok")){
            applyDO.setStatus(2); //审核通过
        }else {
            applyDO.setStatus(3); //审核不通过
        }
        applyDOMapper.updateByPrimaryKeySelective(applyDO);
    }

    /**
     * 发邮件通知学生审核情况
     * @param objectMessage
     */
    @JmsListener(destination = "${MyTopic2}")
    public void sendMailToStu(ObjectMessage objectMessage) throws JMSException {
        CheckDTO checkDTO = (CheckDTO) objectMessage.getObject();
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(checkDTO.getEmail());
            simpleMailMessage.setSentDate(ConvertUtils.formatDate(new Date()));
            simpleMailMessage.setSubject("实验室管理系统");
            if (checkDTO.getStatus().equals("ok")){
                simpleMailMessage.setText("恭喜你,已经被录取,面试时间请注意qq邮箱");
            }else {
                simpleMailMessage.setText("不好意思,你不符合我们的要求,不能通过申请");
            }
            javaMailSender.send(simpleMailMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @JmsListener(destination = "${MyTopic3}")
    public void sendInterviewResultToStu(ObjectMessage objectMessage) throws JMSException {
        System.out.println("消息通知 "+objectMessage.getObject());
        InterviewResultDTO interviewResultDTO = (InterviewResultDTO) objectMessage.getObject();
        //查出所有参加面试的学生Email
        List<EmailDTO> list = applyDOMapper.getAllCheckIsOkStuEmail(interviewResultDTO.getReleasePeople());
        String[] array = new String[list.size()];
        for (int i = 0 ; i < list.size() ; i++){
            array[i] = list.get(i).getEmail();
        }
        for (int i = 0 ; i < array.length ; i++){
            try {
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                simpleMailMessage.setFrom(from);
                simpleMailMessage.setTo(array[i]);
                simpleMailMessage.setSentDate(ConvertUtils.formatDate(new Date()));
                simpleMailMessage.setSubject(interviewResultDTO.getTitle());
                simpleMailMessage.setText(interviewResultDTO.getContent());
                javaMailSender.send(simpleMailMessage);
                System.out.println("通知");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
